package com.tiyende.credit.loop.ui.presenter

import com.tiyende.credit.loop.common.CommonPresenter
import com.tiyende.credit.loop.configs.TiyendeApiPath
import com.tiyende.credit.loop.configs.TiyendeApiUrl
import com.tiyende.credit.loop.configs.TiyendeConfigs
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.utils.helper.TiyendeAnalysisHelper
import com.tiyende.credit.loop.utils.helper.TiyendeConfigsHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject
import java.util.TimeZone


/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/28 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class TiyendeLaunchPresenter(private val iView: IViewContract.ITiyendeLaunchView) : CommonPresenter(iView) {

    /**
     * 获取地区转发
     */
    fun getLaunchTimeZoneAction() {
        val params = JSONObject()
        params.put("p", TiyendeConfigs.TIYENDE_APPNAME)
        params.put("tz", TimeZone.getDefault().id)
        val httpUrl = TiyendeApiUrl.TIYENDE_HOST + TiyendeApiUrl.TIYENDE_TIMEZONE_URL
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showTiyendeLaunchTimeZoneRequestData(data.optString("data"))
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取APP配置信息
     */
    fun getLaunchConfigsAction() {
        val params = JSONObject().apply {
            put("product",TiyendeConfigs.TIYENDE_APPNAME)
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_APP_INFO_PATH)
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                val obj = data.optJSONObject("obj")
                //保存所有数据
                if (obj != null) {
                    // TODO: 测试模拟版本更新
//                    val customize = obj.optJSONObject("customize")
//                    customize?.let {
//                        val customData = it.optJSONObject("customData")
//                        customData?.let {
//                            val testVersion = customData.optJSONObject("release")
//                            testVersion?.put("isMustUpdate",true)
//                            testVersion?.put("versionCode","9")
//                            testVersion?.put("versionName","1.0.9")
////                            testVersion.put("versionRemark","1.A brand new UI design.$2.Update more secure data processing.$3.Resolve known bugs, optimize code, and enhance user experience.")
//                        }
//                    }

                    TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_COMMON,obj.toString())
                }
                //是否强制更新版本
                val isMustUpdate = TiyendeConfigsHelper.isTiyendeUpdateMust()
                if (isMustUpdate){
                    iView.showTiyendeLaunchVersionUpdateDialog()
                    return
                }

                showLaunchIntentPage()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    fun showLaunchIntentPage(){
        //是否显示引导页
        if (!TiyendeSharedHelper.queryTiyendeSharedBoolean(TiyendeSharedKey.TIYENDE_SHOW_GUIDE_PAGE)){
            iView.showTiyendeLaunchGuidePage()
            return
        }

        //已登录 - 进入主页
        if (TiyendeSharedHelper.queryTiyendeSharedBoolean(TiyendeSharedKey.TIYENDE_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            TiyendeAnalysisHelper.initAnalysisLibAtLoginSuccess()
            iView.showTiyendeLaunchMainPage()
            return
        }
        //进入登录页面
        iView.showTiyendeLaunchLoginPage()
    }
}